

import argparse
import sys




from cfcall.utils import load_deploy
from cfcall.etcdb import md5db
from cfcall.utils import cliutils
from cfcall.utils import clisubcmd


arg = cliutils.arg
this_module = sys.modules[__name__]
subcmd = clisubcmd.cfcSubCmd("cfdeploy", this_module)


def do_list(args):
    """List all the software cfdeploy can deploy.
    """
    objs = load_deploy.load_alldeps()
    for dper in objs:
        print dper.name(),":", dper.info()

@arg('--name',
     default=None,
     help="Name of a packages to deploy.")
def do_uninstall(args):
    """Install related packages for software you deploy."""
    objs = load_deploy.load_alldeps()
    namemap = [ (dp.name(), dp) for dp in objs ]
    
    if args.name:
        dp = dict(namemap)[args.name]
        dp.uninstall(args)

@arg('--name',
     default=None,
     help="Name of a packages to deploy.")
def do_showcfg(args):
    """Install related packages for software you deploy."""
    objs = load_deploy.load_alldeps()
    namemap = [ (dp.name(), dp) for dp in objs ]
    
    if args.name:
        dp = dict(namemap)[args.name]
        dp.showcfg(args)

@arg('--name',
     default=None,
     help="Name of a packages to deploy.")
def do_deploy(args):
    """Deploy the packages use chosed cfg."""
    objs = load_deploy.load_alldeps()
    namemap = [ (dp.name(), dp) for dp in objs ]
    
    if args.name:
        dp = dict(namemap)[args.name]
        dp.deploy(args)

@arg('--name',
     default=None,
     help="Name of a packages to deploy.")
def do_fix(args):
    """Deploy the packages use chosed cfg."""
    objs = load_deploy.load_alldeps()
    namemap = [ (dp.name(), dp) for dp in objs ]
    
    if args.name:
        dp = dict(namemap)[args.name]
        dp.fix(args)

@arg('--name',
     default=None,
     help="Name of a packages to deploy.")
def do_check(args):
    """Deploy the packages use chosed cfg."""
    objs = load_deploy.load_alldeps()
    namemap = [ (dp.name(), dp) for dp in objs ]
    
    if args.name:
        dp = dict(namemap)[args.name]
        dp.check(args)

@arg('--name',
     default=None,
     help="Name of a packages to deploy.")
def do_stop(args):
    """Deploy the packages use chosed cfg."""
    objs = load_deploy.load_alldeps()
    namemap = [ (dp.name(), dp) for dp in objs ]
    
    if args.name:
        dp = dict(namemap)[args.name]
        dp.stop(args)

def main():

    md5db.init_db()

    parser = subcmd.get_base_parser()
    (options, xargs) = parser.parse_known_args()
    
    


    subcommand_parser = subcmd.get_subcommand_parser()
    parser = subcommand_parser

    # see if help or less subcmd
    if options.help or not sys.argv[1:]:
        subcommand_parser.print_help()
        return 0
    
    args = subcommand_parser.parse_args()


    # Short-circuit and deal with help right away.

        
    if args.func == subcmd.do_help:
        subcmd.do_help(args)
        return 0
    elif args.func == subcmd.do_bash_completion:
        subcmd.do_bash_completion(args)
        return 0

    args.func(args)




